﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.FPCAutomaticPlateSetting
{
    [FlowAttributes(nameof(TrayStation), 3)]
    public class TrayStation : TaskBase
    {
        #region 轴变量
        #endregion

        #region 上升沿
        private bool _料盘升降轴UpRisingEdge = false, _料盘升降轴DownRisingEdge = false;
        #endregion

        #region 定时器
        private Ton _tonDelay = new Ton();
        #endregion

        #region 点位
        #endregion

        #region 输入点
        private NormalOpen _进料拉带到位感应;
        private NormalOpen _进料拉带存料感应;
        #endregion

        #region 输出点
        private ISingleControl _进料皮带电机;
        #endregion

        #region 报警变量
        #endregion

        #region 私有变量
        #endregion
        public TrayStation(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _进料拉带到位感应 = iop.GetNormalOpen("进料拉带到位感应");
            _进料拉带存料感应 = iop.GetNormalOpen("进料拉带存料感应");

            _进料皮带电机 = _iop.GetSingleControl("进料皮带", "电机");
        }

        public override void EmergencyStop()
        {

        }

        public override void Handle()
        {
            if (_temporary.SystemStatus.Auto)
            {
                if (_进料皮带电机.Manual_Open)
                {
                    _进料皮带电机.Manual_Close = true;
                    _进料皮带电机.Manual_Open = false;
                }
            }
            if (SignalOn(0))
            {
                if (!_进料拉带到位感应.Execute() && !_进料拉带存料感应.Execute())
                {
                    _进料皮带电机.Auto_Open = true;
                    _进料皮带电机.Auto_Close = !_进料皮带电机.Auto_Open;
                    NextStep();
                }
                else if (_temporary.Status_Z轴.Permit_Blowing && _进料拉带存料感应.Execute())
                {
                    _step = 20;
                }
                else if (_temporary.Status_Z轴.Permit_Blowing && _进料拉带到位感应.Execute())
                {
                    _temporary.Status_Z轴.Permit_Blowing = false;
                    _step = 30;
                }
            }
            if (SignalOn(10))
            {
                if (_进料拉带存料感应.Execute())
                {
                    _进料皮带电机.Auto_Open = false;
                    _进料皮带电机.Auto_Close = !_进料皮带电机.Auto_Open;
                    if (!_进料皮带电机.IOStatus)
                    {
                        NextStep();
                    }
                }
            }
            if (SignalOn(20))
            {
                if (_temporary.Status_Z轴.Permit_Blowing)
                {
                    _进料皮带电机.Auto_Open = true;
                    _进料皮带电机.Auto_Close = !_进料皮带电机.Auto_Open;
                    if (_进料皮带电机.IOStatus)
                    {
                        _temporary.Status_Z轴.Permit_Blowing = false;
                        NextStep();
                    }
                }
            }
            if (SignalOn(30))
            {
                if (_进料拉带到位感应.Execute())
                {
                    _tonDelay.Time(200, true);
                    if (_tonDelay.Out)
                    {
                        _tonDelay.Time(200, false);
                        _temporary.Status_Z轴.DischargeComplete = true;
                        _进料皮带电机.Auto_Open = false;
                        _进料皮带电机.Auto_Close = !_进料皮带电机.Auto_Open;
                        NextStep();
                    }
                }
            }
            if (SignalOn(40))
            {
                if (!_进料皮带电机.IOStatus)
                {
                    _step = 0;
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {

            }
        }

        public override void Resetting()
        {

        }

        public override void SafeStop()
        {

        }
    }
}
